package panel.publicPanel
{
	import assists.HtmlText;
	import assists.Scale9Grid;
	import assists.SizeManager;
	import assists.TextFieldUtils;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class NavigationBar extends Sprite
	{
		private var _loader:ResourceLoader;
		private var _stage:Stage;
		private var _selectBtn:Sprite = new Sprite();

		private var _backScrid:Scale9Grid;
		private var _distance:int;
		private var _dataList:Array = [];
		public function NavigationBar(loader:ResourceLoader,stage:Stage)
		{
			_loader = loader;
			_stage = stage;
			init();
		}
		
		private function init():void
		{
			var backBmp:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.guideBack);
			_backScrid = new Scale9Grid(backBmp.bitmapData,20,20);
			_backScrid.height = SizeManager.hasMaxValue(0.15,120);
			_backScrid.width = _stage.stageWidth;
			
			var selectBmp:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.guideSelect);
			selectBmp.height = SizeManager.hasMaxValue(0.15,120);
			_selectBtn.addChild(selectBmp);
			
			this.addChild(_backScrid);
			this.addChild(_selectBtn);
			_backScrid.addEventListener(MouseEvent.CLICK,clickhandler);
		}
		
		protected function clickhandler(event:MouseEvent):void
		{
			var id:int = event.localX / _distance;
			_selectBtn.x = id * _distance;
			_dataList[id].func();
		}
		
		/**
		 * @param dataList {label:"标签名字",func:"触发函数"}
		 */		
		public function show(dataList:Array):void
		{
			_dataList = dataList;
			_distance = _stage.stageWidth / dataList.length;
			_selectBtn.width = _distance;
			for(var i:int=0;i<dataList.length;i++)
			{
				var label:TextField = new TextField();
				label.mouseEnabled = false;
				label.autoSize = "left";
				TextFieldUtils.setHtmlText(label,HtmlText.formatSizeColor(dataList[i].label,0xffffff));
				label.x =  _distance * i  + (_distance - label.width) * 0.5;
				label.y = (_backScrid.height - label.height) * 0.5;
				this.addChild(label);
			}
			drawLine(dataList.length - 1);
		}
		
		private function drawLine(nums:uint):void
		{
			for(var i:int=0;i<nums;i++)
			{
				var line:Bitmap = _loader.uiManager.generateBmp(_loader.uiManager.cutLine,true);
				line.width = _backScrid.height;
				line.rotation = 90;
				line.x = _distance * (i + 1);
				this.addChild(line);
			}
		}
	}
}